﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IMAPIv2Helper;
using System.Runtime.InteropServices;

namespace CheckingMediaSupport
{
  class Program
  {

    // This script examines characteristics of the media loaded in the disc device.
    // Copyright (C) Microsoft Corp. 2006

    // *** IMAPI2 Media Types 
    // SH: Not Needed since C# Can Use Enum IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE
    //const int IMAPI_MEDIA_TYPE_UNKNOWN = 0;             // Media not present OR
    //                                                    // is unrecognized
    //const int IMAPI_MEDIA_TYPE_CDROM = 1;               // CD-ROM
    //const int IMAPI_MEDIA_TYPE_CDR = 2;                 // CD-R
    //const int IMAPI_MEDIA_TYPE_CDRW = 3;                // CD-RW
    //const int IMAPI_MEDIA_TYPE_DVDROM = 4;              // DVD-ROM
    //const int IMAPI_MEDIA_TYPE_DVDRAM = 5;              // DVD-RAM
    //const int IMAPI_MEDIA_TYPE_DVDPLUSR = 6;            // DVD+R
    //const int IMAPI_MEDIA_TYPE_DVDPLUSRW = 7;           // DVD+RW
    //const int IMAPI_MEDIA_TYPE_DVDPLUSR_DUALLAYER = 8;  // DVD+R dual layer
    //const int IMAPI_MEDIA_TYPE_DVDDASHR = 9;            // DVD-R
    //const int IMAPI_MEDIA_TYPE_DVDDASHRW = 10;          // DVD-RW
    //const int IMAPI_MEDIA_TYPE_DVDDASHR_DUALLAYER = 11; // DVD-R dual layer
    //const int IMAPI_MEDIA_TYPE_DISK = 12;               // Randomly writable

    // *** IMAPI2 Data Media States
    // SH: Not Needed since C# Can Use Enum IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN = 0;
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_OVERWRITE_ONLY = 1;
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_BLANK = 2;
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_APPENDABLE = 4;
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_FINAL_SESSION = 8;
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_INFORMATIONAL_MASK = 15;
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_DAMAGED = 1024; //0x400
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_ERASE_REQUIRED = 2048; //0x800
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_NON_EMPTY_SESSION = 4096; //0x1000
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_WRITE_PROTECTED = 8192; //0x2000
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_FINALIZED = 16384; //0x4000
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_UNSUPPORTED_MEDIA = 32768; //0x8000
    //const int IMAPI_FORMAT2_DATA_MEDIA_STATE_UNSUPPORTED_MASK = 61532; //0xfc00

    static void Main(string[] args)
    {

      try
      {

        dynamic Index;              // Index to recording drive.
        IMAPI2.MsftDiscRecorder2 recorder;             // Recorder object
        dynamic Stream;             // Data stream for burning device

        Index = 1;                // Second drive on the system

        // Create a DiscMaster2 object to connect to CD/DVD drives.
        dynamic g_DiscMaster;
        g_DiscMaster = new IMAPI2.MsftDiscMaster2();

        // Create a DiscRecorder object for the specified burning device.
        dynamic uniqueId;
        recorder = new IMAPI2.MsftDiscRecorder2();
        uniqueId = g_DiscMaster.Item(Index);
        recorder.InitializeDiscRecorder(uniqueId);

        // Define the new disc format and set the recorder
        IMAPI2.MsftDiscFormat2Data dataWriter;
        dataWriter = new IMAPI2.MsftDiscFormat2Data();
        dataWriter.Recorder = recorder;

        bool boolResult;

        boolResult = dataWriter.IsRecorderSupported(recorder);
        if (boolResult == true)
        {
          Console.WriteLine("--- Current recorder IS supported. ---");
        }
        else
        {
          Console.WriteLine("Current recorder IS NOT supported.");
        }

        boolResult = dataWriter.IsCurrentMediaSupported(recorder);
        if (boolResult == true)
        {
          Console.WriteLine("--- Current media IS supported. ---");
        }
        else
        {
          Console.WriteLine("Current media IS NOT supported.");
        }

        Console.WriteLine("ClientName = " + dataWriter.ClientName);

        // Check a few CurrentMediaStatus possibilities. Each status
        // is associated with a bit and some combinations are legal.
        IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE curMediaStatus;
        curMediaStatus = dataWriter.CurrentMediaStatus;
        Console.WriteLine("Checking Current Media Status");

        switch (curMediaStatus)
        {
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_APPENDABLE:
            Console.WriteLine("    Media is currently appendable.");
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_BLANK:
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_DAMAGED:
            Console.WriteLine("    Media is damaged.");
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_ERASE_REQUIRED:
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_FINALIZED:
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_FINAL_SESSION:
            Console.WriteLine("    Media is in final writing session.");
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_INFORMATIONAL_MASK:
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_NON_EMPTY_SESSION:
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_OVERWRITE_ONLY:
            Console.WriteLine("    Currently, only overwriting is supported.");
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_UNKNOWN:
            Console.WriteLine("    Media state is unknown.");
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_UNSUPPORTED_MASK:
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_UNSUPPORTED_MEDIA:
            break;
          case IMAPI2.IMAPI_FORMAT2_DATA_MEDIA_STATE.IMAPI_FORMAT2_DATA_MEDIA_STATE_WRITE_PROTECTED:
            break;
          default:
            break;
        }

        IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE mediaType;
        mediaType = dataWriter.CurrentPhysicalMediaType;
        Console.WriteLine("Current Media Type");
        DisplayMediaType(mediaType);

        Console.WriteLine();
        Console.WriteLine("----- Finished -----");

      }
      catch (COMException ex)
      {
        string name = IMAPIReturnValues.GetName(ex.ErrorCode);
        Console.WriteLine("The following COM Exception occured: " + name);
      }

      Console.ReadKey();

    }

    static void DisplayMediaType(IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE mediaType)
    {

      switch (mediaType)
      {
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_BDR:
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_BDRE:
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_BDROM:
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_CDR:
          Console.WriteLine("    CD-R");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_CDROM:
          Console.WriteLine("    CD-ROM");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_CDRW:
          Console.WriteLine("    CD-RW");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DISK:
          Console.WriteLine("    Randomly-writable, hardware-defect managed media type ");
          Console.WriteLine("    that reports the \"Disc\" profile as current.");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDDASHR:
          Console.WriteLine("    DVD-R");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDDASHRW:
          Console.WriteLine("    DVD-RW");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDDASHR_DUALLAYER:
          Console.WriteLine("    DVD-R Dual Layer media");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDPLUSR:
          Console.WriteLine("    DVD+R");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDPLUSRW:
          Console.WriteLine("    DVD+RW");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDPLUSRW_DUALLAYER:
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDPLUSR_DUALLAYER:
          Console.WriteLine("    DVD+R Dual Layer media");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDRAM:
          Console.WriteLine("    DVD-RAM");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DVDROM:
          Console.WriteLine("    Read-only DVD drive and/or disc");
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_HDDVDR:
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_HDDVDRAM:
          break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_HDDVDROM:
          break;
        //case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_MAX:
        //  break;
        case IMAPI2.IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_UNKNOWN:
          Console.WriteLine("    Empty device or an unknown disc type.");
          break;
        default:
          break;
      }
    }
  }
}
